'use strict'

const _ = require('lodash');
const { TaiSpider } = require('tai-spider');

const keywords = [
    'Anticipated Re-entries'
];

const rocdataMap = [{
    fn: 'version',
    dn: 'Version',
}, {
    fn: 'strapOn',
    dn: 'Strap-On'
}, {
    fn: 'stage1',
    dn: 'Stage 1'
}, {
    fn: 'stage2',
    dn: 'Stage 2',
}, {
    fn: 'stage3',
    dn: 'Stage 3',
}, {
    fn: 'stage4',
    dn: 'Stage 4',
}];

const rocperfMap = [{
    dn: 'Performance (kg)',
    fn: 'version'
}, {
    dn: 'LEO',
    fn: 'leo',
}, {
    dn: 'LPEO',
    fn: 'lpeo',
}, {
    dn: 'SSO',
    fn: 'sso',
}, {
    dn: 'GTO',
    fn: 'gto',
}, {
    dn: 'GEO',
    fn: 'geo',
}, {
    dn: 'MolO',
    fn: 'molo',
}, {
    dn: 'IP',
    fn: 'ip',
}]

class GunterRocSpider extends TaiSpider {

    constructor(options = {}) {
        super(options);
        this.name = 'gunter-roc';
        this.debug = true;
        this.start_urls = ['https://space.skyrocket.de/directories/launcher.htm'];
        // this.start_urls = ['https://space.skyrocket.de/doc_lau/falcon-9.htm'];
        // this.start_urls = ['https://space.skyrocket.de/doc_lau/hermes-a1.htm'];
        // this.start_urls = ['https://space.skyrocket.de/doc_lau/black_brant_terrier.htm'];
    }

    *parse(response) {
        for (let ele of response.css('.country-list li')) {
            yield response.follow(ele.css('a'), this.parseCountry);
        }
    }

    *parseCountry(response) {
        for (let ele of response.css('table.index ul li')) {
            yield* response.follow_all(ele.css('a'), this.parseRoc);
        }
    }

   *parseRoc(response) {
    // *parse(response) {
        let rocname = response.css('.container h1').extract_first();

        let description = response.css('#rocdescription >p').extract().join('\n');

        let imglist = []
        for (let imageEle of response.css('#rocdescription img')) {
            let text = imageEle.next('p') ? imageEle.next('p').extract() : '';
            imglist.push({
                url: response.absolute_url(imageEle.attr('src')),
                text,
            });
            yield response.follow(imageEle.attr('src'), this.parseImage, {
                download: true,
                options: {
                    type: 'jpg',
                },
                extData: {
                    rocname,
                }
            });
        };

        for (let imageEle of response.css('table.itab img')) {
            let text = imageEle.next('p') ? imageEle.next('p').extract() : '';
            imglist.push({
                url: response.absolute_url(imageEle.attr('src')),
                text,
            });
            yield response.follow(imageEle.attr('src'), this.parseImage, {
                download: true,
                options: {
                    type: 'jpg',
                },
                extData: {
                    rocname,
                }
            });
        };

        let rocdataEle = response.css('#rocdata').get(0);

        let roclist = rocdataEle ? rocdataEle.parseTable({
            fieldNameMap: rocdataMap
        }) : [];

        let rocperfEle = response.css('#rocperf').get(0);
        let perflist = rocperfEle ? rocperfEle.parseTable({
            fieldNameMap: rocperfMap,
        }) : [];

        perflist.forEach(perf => {
            let items = roclist.filter(item => item.version === perf.version)
            if (items && items.length === 1) {
                _.merge(items[0], perf)
            }
        })

        yield {
            index_name: 'tai-vehicle',
            rocname,
            imglist,
            description,
            id: rocname,
            roclist,
        };
    }

    *parseImage(response) {
        yield {
            index_name: 'tai-file',
            ...response.options.extData,
        }
    }
}

module.exports = GunterRocSpider;